Twitter Activity

Row

Total Tweets

445

Total Unique Users

167

Row

Twitter Activity

Breakdown

Row

User with the most tweets

@AlDatavizguy (125 Tweets)

Date with the most tweets

February 16 (73 Tweets)

Row

Top 10 Users with the Highest Follower Count

Tweets Over Time

Row

Number of Tweets by Hour

Number of Tweets by Day

Tweets

---
title: "#DuboisChallenge Tweets"
author: "Antony Rono"
date: 2021-06-19
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE)

library(flexdashboard)
library(tidyverse)
library(tidytuesdayR)
library(scales)
library(lubridate)
library(plotly)
library(magrittr)
library(ggthemes)

options(scipen = 99)
theme_set(theme_classic())
```

```{r Load, include=FALSE}

#tt <- tt_load("2021-06-15")

tweets <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-06-15/tweets.csv')


```

```{r Cleaning data, include=FALSE}

tweets_cleaned <- tweets %>% 
  
  separate(datetime, into = c("date", "time"), sep = " ", remove = FALSE) %>% 
  
  mutate(date = ymd(date),
         
         datetime = ymd_hms(datetime),
         
         hour = str_extract(floor_date(datetime, "hour"), "[0-9]{2}\\:[0-9]{2}"),
         
         week_day = wday(date, label = TRUE, abbr = FALSE),
         
         username = paste0("@", username)
         
         ) %>% 
  
  select(1:3, hour, week_day, everything()) 

```


```{r map data, include=FALSE}

tweets_map_hc <- tweets_cleaned %>% 
  
  filter(!is.na(long), !is.na(lat)) %>% 
  
  select(location,date, time, username, long, lat, content) %>% 
  
  rename(lon = long)

```

Twitter Activity {data-icon="fa-map-marker-alt"}
=======================================================================

Row {data-height=150}
-----------------------------------------------------------------------

### Total Tweets

```{r total tweets}

valueBox(value = nrow(tweets_cleaned), caption = "Total Tweets", icon = "fa-hashtag", color = "green")

```

### Total Unique Users

```{r total unique users}

valueBox(value = nrow(tweets_cleaned %>% distinct(username)), caption = "Total Unique Users", icon = "fa-users", color = "blue")

```

Row {data-height=850}
-----------------------------------------------------------------------

#### Twitter Activity

```{r twitter activity}

library(highcharter)

#x <- get_data_from_map(download_map_data("https://code.highcharts.com/mapdata/custom/world-highres3.js"))

hcmap(map = "custom/world-highres3") %>% 
  
  hc_add_series(data =tweets_map_hc,
                
                type = "mappoint",
                
                name = "Tweet",
                
                size = "5%",
                
                #minSize = "1%",
                
                #maxSize = "5%",
                
                color = "darkred",
                
                marker = list(radius=6),
                
                tooltip = list(
                  
                  pointFormat = "{point.username}: {point.content} 

Location: {point.location}

Time: {point.time} on {point.date}" ) ) %>% hc_mapNavigation(enabled = FALSE) %>% hc_legend("none") ``` Breakdown {data-icon="fa-chart-bar"} ======================================================================= Row {data-height=150} ----------------------------------------------------------------------- ### User with the most tweets ```{r user with most activity} most_tweets_user <- tweets_cleaned %>% count(username, sort = TRUE) %>% head(1) user <- paste0(most_tweets_user$username, " (", most_tweets_user$n, " Tweets", ")") valueBox(value = user, caption = "User with the most tweets", icon = "fa-user-tag", color = "tomato") ``` ### Date with the most tweets ```{r date with most activity} most_tweets_date <- tweets_cleaned %>% count(date, sort = TRUE) %>% head(1) date <- paste0(format(most_tweets_date$date, "%B %d"), " (", most_tweets_date$n, " Tweets", ")") valueBox(value = date, caption = "Date with the most tweets", icon = "fa-calendar", color = "blueviolet") ``` Row {data-height=400} ----------------------------------------------------------------------- ### Top 10 Users with the Highest Follower Count ```{r users with highest followers} p <- tweets_cleaned %>% count(username, verified, sort = TRUE, name = "count_of_tweets") %>% slice_max(count_of_tweets, n = 10) %>% mutate(username = fct_reorder(username,count_of_tweets )) %>% ggplot(aes(count_of_tweets, username, fill = verified))+ geom_col() + scale_x_continuous(expand = c(0,0)) + labs(y = "", x = "Number of tweets") ggplotly(p, tooltip = c("x", "y")) ``` ### Tweets Over Time ```{r tweets overtime} p <- tweets_cleaned %>% count(date, name = "count_of_tweets") %>% ggplot(aes(date, count_of_tweets)) + geom_line(color = "blueviolet") + geom_point(color = "blueviolet") + scale_x_date(date_labels = "%b %d", date_breaks = "2 weeks") + #theme_classic()+ labs(x = "Date", y = "Number of tweets") ggplotly(p) ``` Row {data-height=450} ----------------------------------------------------------------------- ### Number of Tweets by Hour ```{r tweets by hour} p <- tweets_cleaned %>% count(hour, sort = TRUE, name = "count_of_tweets") %>% filter(!is.na(hour)) %>% ggplot(aes(hour, count_of_tweets, fill = count_of_tweets)) + geom_col() + scale_y_continuous(expand = c(0,0))+ scale_fill_gradient2()+ #coord_flip() + theme(legend.position = "none", axis.text.x = element_text(angle = -45, vjust = -.5) )+ labs(x = "Hour", y = "Number of tweets") ggplotly(p, tooltip = c("x", "y")) ``` ### Number of Tweets by Day ```{r tweets by day} p <- tweets_cleaned %>% count(week_day, sort = TRUE, name = "count_of_tweets") %>% filter(!is.na(week_day)) %>% ggplot(aes(week_day, count_of_tweets, fill = count_of_tweets)) + geom_col() + scale_y_continuous(expand = c(0,0))+ scale_fill_gradient2()+ #coord_flip() + theme(legend.position = "none")+ labs(x = "Day", y = "Number of tweets") ggplotly(p, tooltip = c("x", "y")) ``` Tweets {data-icon="fa-twitter"} ======================================================================= ```{r DT Table Funcctions, include=FALSE} # Function to add color bar format_color_bar <- function(..., col_name, col){ formatStyle(..., columns = deparse(substitute(col_name)), background = styleColorBar(col, 'coral'), backgroundSize = '100% 90%', backgroundRepeat = 'no-repeat', backgroundPosition = 'center' ) } ``` ```{r tweets with highest interaction} library(DT) tweets_highest_interactions <- tweets_cleaned %>% rowwise() %>% mutate(activity = sum(c_across(ends_with("count"))), #username = paste0("@", username), url = paste0("Link") ) %>% arrange(desc(activity)) %>% #head(20) %>% select(date, time,username, content,ends_with("count"), url ) %>% magrittr::set_colnames(c("Date","Time", "Username", "Tweet","Retweets", "Likes", "Quote", "Link")) tweets_highest_interactions %>% datatable(extensions = 'Buttons', class = "display", rownames = FALSE, escape = FALSE, options = list(dom = 'Blfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), lengthMenu = list(c(10,25,50,-1), c(10,25,50,"All") ) ) ) %>% format_color_bar(., col_name = Retweets, col = tweets_highest_interactions$Retweets) %>% format_color_bar(., col_name = Likes, col = tweets_highest_interactions$Likes) %>% format_color_bar(., col_name = Quote, col = tweets_highest_interactions$Quote) ```